home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / hard / drivr / cyberx10.lha / CyberX10 / Source / serial.c < prev    next >
C/C++ Source or Header  |  1992-11-06  |  5KB  |  204 lines

  1.  
  2.  /* storage for various magic items */
  3.  
  4. struct Library *OwnDevUnitBase;
  5.  
  6. static struct IOExtSer SerialRead;
  7. static struct IOExtSer SerialWrite;
  8. static struct MsgPort *SReadPort;
  9. static struct MsgPort *SWritePort;
  10. static ULONG SReadSigMask;
  11. static ULONG SWriteSigMask;
  12.  
  13. static UBYTE SReadBuf[256];
  14. static ULONG SReadPos;
  15. static ULONG SReadSize;
  16.  
  17. static UBYTE SWriteBuf[256];
  18. static ULONG SWritePos;
  19.  
  20. static STRPTR SerDevice;
  21. static ULONG SerUnit;
  22.  
  23.  /* open up the serial device */
  24.  
  25. BOOL OpenSerial(STRPTR Device, ULONG Unit, BOOL AttemptMode)
  26. {
  27.     register struct IOExtSer *ior = &SerialRead;
  28.     register struct IOExtSer *iow = &SerialWrite;
  29.  
  30.     STRPTR ODUresult;
  31.  
  32.     /* save these away for when we need to close the device */
  33.  
  34.     SerDevice = Device;
  35.     SerUnit = Unit;
  36.  
  37.     /* allocate our message ports */
  38.     if (!(SReadPort = CreatePort(NULL, 0)))
  39.         return FALSE;
  40.  
  41.     if (!(SWritePort = CreatePort(NULL, 0))) {
  42.         DeletePort(SReadPort);
  43.         return FALSE;
  44.     }
  45.  
  46.     SReadSigMask = 1L << SReadPort->mp_SigBit;
  47.     SWriteSigMask = 1L << SWritePort->mp_SigBit;
  48.  
  49.     /*
  50.      * if OwnDevUnit.library is around then use it to try and lock the
  51.      * port
  52.      */
  53.  
  54.     if (OwnDevUnitBase = OpenLibrary(ODU_NAME, 0)) {
  55.         if (AttemptMode)
  56.             ODUresult = AttemptDevUnit(Device, Unit, PROGNAME, 0L);
  57.         else
  58.             ODUresult = LockDevUnit(Device, Unit, PROGNAME, 0L);
  59.  
  60.         if (ODUresult) {
  61.             CloseLibrary(OwnDevUnitBase);
  62.             DeletePort(SWritePort);
  63.             DeletePort(SReadPort);
  64.             return FALSE;
  65.         }
  66.     }
  67.  
  68.     /* try and open the device */
  69.     ior->io_SerFlags = SERF_XDISABLED;
  70.     ior->IOSer.io_Message.mn_ReplyPort = SReadPort;
  71.  
  72.     if (OpenDevice(Device, Unit, (struct IORequest *) ior, 0)) {
  73.         if (OwnDevUnitBase) {
  74.             FreeDevUnit(Device, Unit);
  75.             CloseLibrary(OwnDevUnitBase);
  76.         }
  77.  
  78.         DeletePort(SWritePort);
  79.         DeletePort(SReadPort);
  80.         return FALSE;
  81.     }
  82.  
  83.     ior->IOSer.io_Command = SDCMD_QUERY;
  84.     DoIO((struct IORequest *) ior);
  85.  
  86.     ior->io_Baud = 600;
  87.     ior->io_ReadLen = ior->io_WriteLen = 8;
  88.     ior->io_StopBits = 1;
  89.     ior->IOSer.io_Command = SDCMD_SETPARAMS;
  90.     if (DoIO((struct IORequest *) ior)) {
  91.         CloseSerial();
  92.         return FALSE;
  93.     }
  94.  
  95.     CopyMem((APTR) ior, (APTR) iow, sizeof(struct IOExtSer));
  96.     iow->IOSer.io_Message.mn_ReplyPort = SWritePort;
  97.  
  98.     SReadPos = SReadSize = 0;
  99.  
  100.     return TRUE;
  101. }
  102.  
  103.  /* close down the serial handling stuff */
  104.  
  105. void CloseSerial(void)
  106. {
  107.     CloseDevice((struct IORequest *) & SerialRead);
  108.  
  109.     if (OwnDevUnitBase) {
  110.         FreeDevUnit(SerDevice, SerUnit);
  111.         CloseLibrary(OwnDevUnitBase);
  112.     }
  113.  
  114.     DeletePort(SWritePort);
  115.     DeletePort(SReadPort);
  116. }
  117.  
  118.  /* flush out the serial read buffer */
  119.  
  120. void ClearSerial(void)
  121. {
  122.     SReadPos = SReadSize = 0;
  123.  
  124.     SerialRead.IOSer.io_Command = CMD_CLEAR;
  125.     DoIO((struct IORequest *) & SerialRead);
  126. }
  127.  
  128.  /* send a string to the serial device */
  129.  
  130. void SerWrite(STRPTR Buf, ULONG Len)
  131. {
  132.     SerialWrite.IOSer.io_Command = CMD_WRITE;
  133.     SerialWrite.IOSer.io_Length = Len;
  134.     SerialWrite.IOSer.io_Data = (APTR) Buf;
  135.     DoIO((struct IORequest *) & SerialWrite);
  136. }
  137.  
  138.  /* get a character from the serial device with a timeout */
  139.  
  140. UWORD SerGetRawChar(ULONG timeout)
  141. {
  142.     UBYTE Buffer[2];
  143.     ULONG Signals;
  144.  
  145.     if (SReadPos < SReadSize)
  146.         return (UWORD) SReadBuf[SReadPos++];
  147.  
  148.     SetSignal(0L, SReadSigMask | TimerSigMask);
  149.  
  150.     SerialRead.IOSer.io_Command = CMD_READ;
  151.     SerialRead.IOSer.io_Length = 1;
  152.     SerialRead.IOSer.io_Data = (APTR) Buffer;
  153.     SendIO((struct IORequest *) & SerialRead);
  154.  
  155.     TimerIO.tr_node.io_Command = TR_ADDREQUEST;
  156.     TimerIO.tr_time.tv_secs = timeout;
  157.     TimerIO.tr_time.tv_micro = 0;
  158.     SendIO((struct IORequest *) & TimerIO);
  159.  
  160.     Signals = Wait(SReadSigMask | TimerSigMask);
  161.  
  162.     if (Signals & SReadSigMask) {
  163.         if (!CheckIO((struct IORequest *) & TimerIO)) {
  164.             AbortIO((struct IORequest *) & TimerIO);
  165.         }
  166.         WaitIO((struct IORequest *) & TimerIO);
  167.  
  168.         WaitIO((struct IORequest *) & SerialRead);
  169.  
  170.         if (!SerialRead.IOSer.io_Error && SerialRead.IOSer.io_Actual) {
  171.             SerialRead.IOSer.io_Command = SDCMD_QUERY;
  172.             DoIO((struct IORequest *) & SerialRead);
  173.  
  174.             if (SerialRead.IOSer.io_Actual) {
  175.                 SerialRead.IOSer.io_Command = CMD_READ;
  176.                 SerialRead.IOSer.io_Length = min(sizeof(SReadBuf), SerialRead.IOSer.io_Actual);
  177.                 SerialRead.IOSer.io_Data = (APTR) SReadBuf;
  178.                 DoIO((struct IORequest *) & SerialRead);
  179.  
  180.                 if (!SerialRead.IOSer.io_Error) {
  181.                     SReadPos = 0;
  182.                     SReadSize = SerialRead.IOSer.io_Actual;
  183.                 }
  184.             }
  185.  
  186.             return (UWORD) Buffer[0];
  187.         }
  188.         else
  189.             return 0;
  190.     }
  191.  
  192.     if (Signals & TimerSigMask)
  193.         WaitIO((struct IORequest *) & TimerIO);
  194.  
  195.     /* if we get down here we have timed out waiting for a character */
  196.  
  197.     if (!CheckIO((struct IORequest *) & SerialRead)) {
  198.         AbortIO((struct IORequest *) & SerialRead);
  199.     }
  200.     WaitIO((struct IORequest *) & SerialRead);
  201.  
  202.     return TIMEOUT;
  203. }
  204.